package com.xand.bt.terminal.ui.depth;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import com.xand.bt.terminal.util.BitcoinTerminalConst;

public class MarketDepthTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1481665232698089698L;
	
	private static final int ROW_NUM			= 20;
	
	public static final int COLUMN_BID_VOLUME 	= 0;
	public static final int COLUMN_BID_PRICE 	= 1;
	public static final int COLUMN_ASK_PRICE 	= 2;
	public static final int COLUMN_ASK_VOLUME 	= 3;
	
	private static final String[] COLUMN_NAMES = {"Bid volume", "Bid price", "Ask price", "Ask volume"};
	
	private List<MarketDepthLevel> bid;
	private List<MarketDepthLevel> ask;
	
	public MarketDepthTableModel() {
		this.bid = new ArrayList<MarketDepthLevel>();
		this.ask = new ArrayList<MarketDepthLevel>();
	}
	
	public List<MarketDepthLevel> getLevels(int direction) {
		
		List<MarketDepthLevel> ret;
		
		if(direction == BitcoinTerminalConst.MARKET_LEVEL_DIRECTION_ASK) {
			
			ret = this.ask;
			
		}else{
			
			ret = this.bid;
			
		}
		
		return ret;
	}
	
	public boolean isCellEditable(int row, int column) {
		return false;
	}
	
	public String getColumnName(int index) {
		String ret = COLUMN_NAMES[index];
		
		return ret;
	}
	
	public int getRowCount() {
		
		return ROW_NUM;
		
	}
	
	public int getColumnCount() {
		
		return COLUMN_NAMES.length;
		
	}
	
	public Object getValueAt(int rowIndex, int columnIndex) {
		String ret = "";
		
		switch(columnIndex) {
		
			case COLUMN_BID_VOLUME:
				
				if(rowIndex < this.bid.size()) {
					ret = this.bid.get(rowIndex).getVolume();
				}
				
				break;
				
			case COLUMN_BID_PRICE:
				
				if(rowIndex < this.bid.size()) {
					ret = this.bid.get(rowIndex).getPrice();
				}
				
				break;
				
			case COLUMN_ASK_PRICE:
				
				if(rowIndex < this.ask.size()) {
					ret = this.ask.get(rowIndex).getPrice();
				}
				
				break;
				
			case COLUMN_ASK_VOLUME:
				
				if(rowIndex < this.ask.size()) {
					ret = this.ask.get(rowIndex).getVolume();
				}
				
				break;
				
		}
		
		return ret;
	}
}
